package com.newtype.meeting.client.widget.backend;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ListLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanelSelectionModel;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.GMeeting;
import com.newtype.meeting.client.model.Department;
import com.newtype.meeting.client.service.DepartmentServiceAsync;

/**
 * 部门管理面板控件
 * @author 汪恭泽
 * May 31, 2011 11:01:47 AM
 */
public class DepartmentManagementPanel extends ContentPanel {
	private TreePanel<ModelData> treePanel;
	private ContentPanel centerPanel;
	private ContentPanel westPanel;
	private TreeStore<ModelData> store = new TreeStore<ModelData>();

	public DepartmentManagementPanel() {
		setHeaderVisible(false);
		setLayout(new BorderLayout());
		setBodyBorder(false);
		setIconStyle("icon-sytem");

		createWestPanel();
		createCenterPanel();
	}

	private void createCenterPanel() {
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(0));
		centerPanel = new ContentPanel();
		centerPanel.setHeaderVisible(false);
		centerPanel.setLayout(new FitLayout());

		//gwt service
		final DepartmentServiceAsync service = Registry
				.get(GMeeting.DEPARTMENT_SERVICE);
		//proxy and reader
		RpcProxy<List<Department>> proxy = new RpcProxy<List<Department>>() {
			@Override
			protected void load(Object loadConfig,
					AsyncCallback<List<Department>> callback) {
				service.getDepartmentChildren(new Department(), callback);
			}
		};
		//loader and store
		ListLoader<ListLoadResult<ModelData>> loader = new BaseListLoader<ListLoadResult<ModelData>>(
				proxy);
		ListStore<Department> store = new ListStore<Department>(loader);

		loader.load();

		//column model
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(new ColumnConfig("deptName", "部门", 200));

		ColumnModel cm = new ColumnModel(columns);

		Grid<Department> grid = new Grid<Department>(store, cm);
		grid.setAutoExpandColumn("deptName");

		centerPanel.add(grid);

		ToolBar headToolBar = new ToolBar();
		Button btnAddDept = new Button("新增部门");
		btnAddDept.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ToDeptAddWindow);
			}
		});

		Button btnAddPers = new Button("新增人员");
		btnAddPers.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ToAddNewAccountWindow);
			}
		});
		headToolBar.add(btnAddDept);
		headToolBar.add(btnAddPers);

		centerPanel.setTopComponent(headToolBar);

		add(centerPanel, centerData);
	}

	private void createWestPanel() {
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST,
				200, 110, 300);
		westData.setSplit(true);
		westData.setCollapsible(true);
		westData.setMargins(new Margins(0, 5, 0, 0));

		westPanel = new ContentPanel();
		westPanel.setHeading("选择其他部门");

		treePanel = new TreePanel<ModelData>(store);
		treePanel.setDisplayProperty("name");

		TreePanelSelectionModel<ModelData> sm = new TreePanelSelectionModel<ModelData>() {
			@Override
			protected void onSelectChange(ModelData model, boolean select) {
				if (select) {
					MessageBox.alert("提示", model.get("id") + ":"
							+ model.get("name"), null);
				}
			}
		};
		treePanel.setSelectionModel(sm);
		westPanel.add(treePanel);
		add(westPanel, westData);
	}

	public TreeStore<ModelData> getStore() {
		return store;
	}
}
